package com.article.controller;


import com.article.entity.dto.DeleteDao;
import com.article.entity.dto.IdsDao;
import com.article.entity.pojo.ColumnList;
import com.article.entity.vo.ColumnListVo;
import com.article.result.Result;
import com.article.service.ColumnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/column")
public class ColumnController {
    @Autowired
    private ColumnService columnService;


    /**
     * 获取栏目信息
     */
    @RequestMapping()
    public Result getColumn(@RequestParam(value = "id") int pid, @RequestParam(value = "pageNo", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int size) {
        List<ColumnListVo> result = columnService.getColumn(pid, page, size);

        return Result.success(result);
    }

    @PostMapping("/add")
    public Result addColumn(@RequestBody ColumnList columnList) {
        Boolean flag = columnService.insetColumn(columnList);
        if (flag) {
            return Result.success();
        } else {
            return Result.error("添加失败");
        }
    }

    @DeleteMapping("/delete")
    public Result deleteColumn(@RequestBody DeleteDao ids) {
        log.info("删除的id为{}", ids.getIds());
        IdsDao idList = new IdsDao();
        idList.setIds(ids.getIds());
        Boolean flag = columnService.deleteColumn(idList);
        if (flag) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }

    @PostMapping("/update")
    public Result updateColumn(@RequestBody ColumnList columnList) {
        Boolean flag = columnService.updateColumn(columnList);
        if (flag) {
            return Result.success();
        } else {
            return Result.error("修改失败");
        }
    }

    @GetMapping("/number")
    public Result getColumnNumber() {
        int total = columnService.getColumnNumber();
        return Result.success(total);
    }

    @GetMapping("/search")
    public Result searchColumn(@RequestParam(value = "name") String name, @RequestParam(value = "pid", defaultValue = "0") int pid, @RequestParam(value = "pageNo", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "3") int size) {
        log.info("搜索的name为:{}", name);

        List<ColumnListVo> result = columnService.searchColumnByName(name, pid, page, size);
        return Result.success(result);
    }

    @GetMapping("/search/number")
    public Result searchColumnNumber(@RequestParam(value = "name") String name, @RequestParam(value = "pid", defaultValue = "0") int pid) {
        int total = columnService.getColumnNumber1(name, pid);
        return Result.success(total);

    }
}
